<template>
    <div>
      <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline" ref="queryForm">
        <el-form-item label="流量方向" >
          <el-select v-model="searchInfo.netput" clearable placeholder="请选择">
            <el-option
                    v-for="(dict,index) in putOptions"
                    :key="index"
                    :label="dict.item_name"
                    :value="dict.item_value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主机号" prop="virtual_code" >
          <el-input placeholder="搜索条件" v-model="searchInfo.virtual_code" clearable></el-input>
        </el-form-item>
        <el-form-item label="启用状态" prop="status">
          <el-select v-model="searchInfo.status" placeholder="请选择" clearable>
            <el-option key="true" label="正常" value="true"></el-option>
            <el-option key="false" label="关闭" value="false"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" icon="el-icon-search" type="primary">查询</el-button>
        </el-form-item>
      </el-form>
      <el-form>
        <el-form-item>
          <el-button @click="createSecurityDetail()" type="primary">新增安全策略</el-button>
        </el-form-item>
      </el-form>
      </div>

      <el-table
      :data="tableData"
      border
      ref="multipleTable"
      stripe
      style="width: 100%"
      tooltip-effect="dark"
    >
      <el-table-column label="ID" prop="ID" width="50"></el-table-column>
      <el-table-column label="ip" prop="ip" width="150"></el-table-column>
      <el-table-column label="协议" prop="protocol" width="120"></el-table-column>
      <el-table-column label="端口" prop="port" width="100"></el-table-column>
      <el-table-column label="流量方向" prop="uput" width="120"></el-table-column>
      <el-table-column label="内网端口" prop="neiport" width="120"></el-table-column>
      <el-table-column label="启用状态" prop="status" width="120">
        <template slot-scope="scope">{{scope.row.status|formatBoolean}}</template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" width="220"></el-table-column>
      <el-table-column label="按钮组">
        <template slot-scope="scope">
          <el-button @click="updateSecurityDetail(scope.row)" icon="el-icon-edit" size="small" type="primary">编辑</el-button>
          <el-button @click="deleteSecurityDetail(scope.row)" type="danger" icon="el-icon-delete" style="margin-left: 6px;" size="mini" slot="reference">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="600px" >
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="110px">
        <el-form-item label="虚拟主机号" prop="dict_code">
          <el-input
            v-model="formData.virtual_code"
            disabled
            placeholder="请输入virtual_code"
            clearable
            :style="{width: '90%'}"
          ></el-input>
        </el-form-item>
        <el-form-item label="ip地址" prop="ip">
          <el-input
            v-model="formData.ip"
            placeholder="请输入ip"
            clearableport
            :style="{width: '90%'}"
          ></el-input>
        </el-form-item>
        <el-form-item label="请输入端口" prop="port">
          <el-input
            v-model="formData.port"
            placeholder="请输入端口"
            clearable
            :style="{width: '90%'}"
          ></el-input>
        </el-form-item>
        <el-form-item label="请输入流量方向" prop="uput">
          <el-select  placeholder="流量方向" v-model="formData.uput" :style="{width: '90%'}" >
                  <el-option
                    v-for="(item, i) in putOptions"
                    :key="i"
                    :label="item.item_name"
                    :value="item.item_value"
                  ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="请输入内网端口" prop="netput">
          <el-input
            v-model="formData.neiport"
            placeholder="请输入流量方向"
            clearable
            :style="{width: '90%'}"
          ></el-input>
        </el-form-item>
        <el-form-item label="请输入协议类型">
          <el-select  placeholder="协议类型" v-model="formData.protocol" :style="{width: '90%'}" >
                  <el-option
                    v-for="(item, i) in secpOptions"
                    :key="i"
                    :label="item.item_name"
                    :value="item.item_value"
                  ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="启用状态" prop="status" required>
          <el-switch v-model="formData.status" active-text="开启" inactive-text="停用"></el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" clearable type="textarea" :style="{width: '90%'}"></el-input>
      </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>
    </div>
</template>

<script>
  import {
    getSecurityList,
    createSecurityDetail,
    updateSecurityDetail,
    deleteSecurityDetail,
} from "@/api/cmdb_security";
  import infoList from "@/mixins/infoList";
  export default {
    name: "virtualSecurity",
    mixins: [infoList],
     data(){
        return {
          listApi: getSecurityList,
          putOptions:'',
          secpOptions:'',
          dialogFormVisible: false,
          dialogTitle: "新增安全策略",
          visible: false,
          type: "",
          searchInfo: {
            id:undefined,
            virtual_code: undefined,
            ip: undefined,
            status: undefined,
            port: undefined,
            uput: undefined,
            protocol: undefined,
            remark: undefined,
            neiport: undefined,
            },
          formData: {
            id:undefined,
            virtual_code: undefined,
            ip: undefined,
            status: undefined,
            port: undefined,
            uput: undefined,
            protocol: undefined,
            remark: undefined,
            neiport: undefined,
          },
          rules: {
            virtual_code: [{ pattern: /^[0-9]*$/, required: true, message: "请输入虚拟主机号", trigger: "blur" }],


        },
        }
    },
    methods: {
      createSecurityDetail(){
        this.dialogTitle = '新增安全策略'
        this.type = 'create';
        this.dialogFormVisible = true;
        this.formData = {
        virtual_code: this.$route.params.id,
        ip: null,   // 标签名字
        port: null,  // 字典标签值
        uput: null,
        protocol: null,
        status: true,
        remark: null,
        neiport:null,
        };
      },
      updateSecurityDetail(row){
        this.type = "update";
        this.formData = row
        this.dialogFormVisible = true;
      },
      handleQuery(){
        this.page = 1
        this.pageSize = 100
        if (this.searchInfo.status==""){
          this.searchInfo.status=null
        }
        this.getTableData()
      },
      closeDialog() {
      this.dialogFormVisible = false;
      this.formData = {
        dict_code: this.$route.params.id,
        item_name: null,   // 标签名字
        item_value: null,  // 字典标签值
        status: true,
        remark: null
        };
      },
      deleteSecurityDetail(row) {
        this.$confirm("此操作将永久删除该字典项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
        }).then(async () => {
                this.visible = false;
                const res = await deleteSecurityDetail({ ID: row.ID });
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: "删除成功!"
                  });
                  await this.getTableData();
                }
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消删除"
                });
              });
        },
        async enterDialog() {
        this.formData.sysDictionaryID = Number(this.$route.params.id)
        this.$refs['elForm'].validate(async valid => {
        if (!valid) return
          let res;
          switch (this.type) {
          case "create":
            res = await createSecurityDetail(this.formData);
            break;
          case "update":
            res = await updateSecurityDetail(this.formData);
            break;
          default:
            res = await createSecurityDetail(this.formData);
            break;
        }
        if (res.code == 0) {
          this.$message({
            type:"success",
            message:"创建/更改成功"
          })
          this.closeDialog();
           await this.getTableData();
        }
      })

        },

    },
    async created() {
      await this.getTableData();
      this.searchInfo.virtual_code = this.$route.params.id
      this.formData.virtual_code = this.$route.params.id
      this.handleQuery();
      this.putOptions = this.getDicItemList('security_put')
      this.secpOptions = this.getDicItemList('sec_protocol')
  },
  filters: {
    // formatDate: function(time) {
    //   if (time != null && time != "") {
    //     var date = new Date(time);
    //     return formatTimeToStr(date, "yyyy-MM-dd hh:mm:ss");
    //   } else {
    //     return "";
    //   }
    // },
    formatBoolean: function(bool) {
      if (bool != null) {
        return bool ? "正常" : "关闭";
      } else {
        return "";
      }
    },
  },
  }
</script>

<style scoped>

</style>
